package com.wlt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.pojo.Goods;
import com.wlt.result.BaseResult;
import com.wlt.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/goods")
public class GoodsController
{
    @DubboReference
    private GoodsService goodsService;
    
    /**
     * 新增商品
     * @param goods 商品对象
     * @return      执行结果
     */
    @PostMapping ("/add")
    public BaseResult add (@RequestBody Goods goods) throws IOException {
        goodsService.add(goods);
        
        return BaseResult.success();
    }
    
    /**
     * 商品对象
     * @param goods 商品对象
     * @return      执行结果
     */
    @PutMapping ("/update")
    public BaseResult update (@RequestBody Goods goods)
    {
        goodsService.update(goods);
        
        return BaseResult.success();
    }
    
    /**
     * 商品上下架
     * @param id            商品id
     * @param isMarketable  商品状态
     * @return              执行结果
     */
    @PutMapping ("/putAway")
    public BaseResult putAway (Long id, Boolean isMarketable) throws IOException {
        goodsService.putAway(id, isMarketable);
        
        return BaseResult.success();
    }
    
    /**
     * 根据id查询
     * @param id    商品id
     * @return      执行结果
     */
    @GetMapping("/findById")
    public BaseResult<Goods> findById (Long id)
    {
        Goods byId = goodsService.findById(id);
        
        return BaseResult.success(byId);
    }
    
    /**
     * 分页查询
     * @param goods 商品对象
     * @param page  页码
     * @param size  每页条数
     * @return      查询结果
     */
    @GetMapping ("/search")
    public BaseResult<Page<Goods>> search (Goods goods, int page, int size)
    {
        Page<Goods> search = goodsService.search(goods, page, size);
        
        return BaseResult.success(search);
    }
}
